﻿using BLL;
using Config;
using DBUtility;
using gfLibrary;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IYAKASAPortal.Login
{
    public partial class LoginSelect : System.Web.UI.Page
    {
        private static string wrdkey { get; set; }

        private static string drpkey { get; set; }

        private static string info { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                drpkey = "";
                wrdkey = "";
                btnSearchALL_Click(sender, e);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            drpkey = drpJYLB.SelectedValue;
            wrdkey = txtKeywords.Text;
            SelectSql(drpkey, wrdkey);
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string strUniqueKey = txtUniqueKey.Value;
            if ((strUniqueKey == null) || (strUniqueKey.Length == 0) || (gfValidate.Is_Integer(strUniqueKey) == false))
            {
                gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "请选择一条记录！");
                return;
            }
            VEHICLE_DISPATCH_BLL dispatch = new VEHICLE_DISPATCH_BLL();
            VEHICLE_DISPATCH m_Dispatch = new VEHICLE_DISPATCH();
            int dispatchId = int.Parse(strUniqueKey);
            string strJCLSH = "";
            strJCLSH = DbHelper.GetValue("select jclsh from VEHICLE_DISPATCH where id='" + dispatchId + "' and JCZT_STATUS ='0'");
            
            if (!string.IsNullOrEmpty(strJCLSH))
            {
                dispatch.Delete(strJCLSH);
            }
            else
            {
                gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "删除记录失败!");
            }
            Response.Redirect(Request.Url.ToString());
        } 

        protected void GreatPager1_PageIndexChanging(object sender, gfPage.PagerEventArgs e)
        {
            GreatPager1.PageIndex = e.NewPageIndex;

            DataTable m_dtTable = GreatPager1.BindDataTablePage();
            rptList.DataSource = m_dtTable;
            rptList.DataBind();
        }

        protected void btnSearchALL_Click(object sender, EventArgs e)
        {
            drpkey = "";
            wrdkey = "";
            drpJYLB.SelectedIndex = 0;
            txtKeywords.Text = "";
            SelectSql(drpkey, wrdkey);
        }

        protected void DropDownListSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            drpkey = drpJYLB.SelectedValue;
            SelectSql(drpkey, wrdkey);
        }

        private void SelectSql(string strDrp,string strKey)
        {
            string strSql = "select ID,HPHM,HPZL,HPZLDH,JCXH,GWBH,ZYWLB,JYLB,JCZT_STATUS,JYXM FROM VEHICLE_DISPATCH WHERE JCZT_STATUS='0' and JCLSH like '"+strDrp+"%'";
            if (strKey.Length > 0)
            {
                strSql += "and ";
                strSql += "(HPHM like '%" + strKey + "%'";
                strSql += " or VIN like '%" + strKey + "%')";
            }

            strSql += " order by ID desc";
            
            DataTable m_dtTable = new DataTable();
            GreatPager1.BindDataTablePage(ref m_dtTable, AppConfig.g_strConnectionStringSqlClient, strSql, 20);
            rptList.DataSource = m_dtTable;
            rptList.DataBind();
            m_dtTable.Dispose();
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "xxxe", "getSelect();", true);
        
        }

        protected string getJCXM(string jcxm)
        {
            string result = "";
            if (!string.IsNullOrEmpty(jcxm))
            {
                string temp = jcxm.Substring(0, jcxm.Length - 1);
                if (temp.IndexOf(',') > 0)
                {
                    var item = temp.Split(',');
                    foreach (var i in item)
                    {
                        result += getXMName(i.ToString()) + "、";
                    }
                }
                else
                {
                    result = getXMName(temp) + "、";
                }
                return result.Substring(0, result.Length - 1);
            }
            else
            {
                return result;
            }
        }

        protected string getXMName(string xm)
        {
            string result = "";
            result = DbHelper.GetValue("SELECT CON_CON from const_define where CON_CHNAME='jyxm' and CON_CODE='" + xm + "'");
            return result;
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            drpkey = drpJYLB.SelectedValue;
            wrdkey = txtKeywords.Text;
            this.SelectSql(drpkey, wrdkey);         
        }

    }
}